package it.disim.tlp.webreputation.business.impl;

import it.disim.tlp.webreputation.business.UtenteService;
import it.disim.tlp.webreputation.business.model.Utente;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class JDBCUtenteService implements UtenteService {

	/**
	 * The method takes all the information about a user by passing an object with only the id set.
	 * @param Utente An object of type User () "filled" (With inside the id of the user).
	 * @return Utente Returns an object of type User () filled with the data in the DB.
	 */
	@Override
	public Utente getUser(Utente user) {
		
		Connection con = DBConnect.getConnection();
		
		Statement st = null;
		ResultSet rs = null;
		Utente result = null;
		try {
			st = con.createStatement();			

			rs = st.executeQuery("select * from utenti where ID = " + user.getId());
			if (rs.next()) {													
				
				int id = rs.getInt("ID");
				String nome = rs.getString("Nome");	
				String cognome = rs.getString("Cognome");
				String email = rs.getString("Email");
				String password = rs.getString("Password");
				Date data_nascita = rs.getDate("Data_nascita");
				String citta = rs.getString("Citta");
				Date data_ins = rs.getDate("Data_ins");
				boolean admin = rs.getBoolean("Admin");
				boolean attivo = rs.getBoolean("Attivo");
				
				
				result = new Utente(id, nome, cognome, email, password, data_nascita, citta, data_ins, admin, attivo);
			}
			

		} catch (SQLException e) {
			e.printStackTrace();			
		} finally {
			if (st!=null) {
				try {st.close();} catch (SQLException e) {}
			}

		}
		return result;
		
	}

}
